//	processTable.cc

//#include "copyright.h"
#include "processTable.h"

ProcessTableEntry::ProcessTableEntry(AddrSpace *space) {
	addrspace = space;
	activeNum = 0;
	inactiveNum = 0;
	threadMap = new BitMap(MAX_THREAD_NUM);
	entryLock = new Lock("processTableEntryLock");
}

ProcessTableEntry::~ProcessTableEntry() {
	DEBUG('w', "Clean up the process table entry\n");
	delete threadMap;
	delete addrspace;
	delete entryLock;
	addrspace = NULL;
}

int
ProcessTableEntry::CreateThread(int stackNum) {
	int threadId = -1;
	entryLock->Acquire();
	threadId = threadMap->Find();
	if (threadId >= 0) {
		threadInfo[threadId] = stackNum;
		activeNum ++;
	}
	entryLock->Release();
	return threadId;
}

void
ProcessTableEntry::SetStackInfo(int threadId, int stackNum) {
	entryLock->Acquire();
	threadInfo[threadId] = stackNum;
	entryLock->Release();
}

int
ProcessTableEntry::ExitThread(int threadId) {
	if (threadId < 0) {
		return -1;
	}
	entryLock->Acquire();
	threadMap->Clear(threadId);
	activeNum --;
	addrspace->FreeStack(threadInfo[threadId]);
	entryLock->Release();
	return 0;
}
